
import 'package:trade/common/service/i_service.dart';
import 'package:trade/common/util/exchange_logger/exchange_logger.dart';
import 'package:shared_preferences/shared_preferences.dart';

class WDSharedPreferences extends IService {
  late SharedPreferences _sp;
  /// 存储
  void setValue(String key, dynamic value){
    String type = value.runtimeType.toString().toLowerCase();
    switch(type){
      case 'string':
        _sp.setString(key, value);
        break;
      case 'int':
        _sp.setInt(key, value);
        break;
      case 'double':
        _sp.setDouble(key, value);
        break;
      case 'bool':
        _sp.setBool(key, value);
        break;
      default:
        logger.d('类型错误: type = $type');
        break;
    }
  }
  /// 获取
  dynamic getValue(String key){
    if(isKeyExisted(key) == false) return null;
    return _sp.get(key);
  }

  /// 删除
  Future<bool> removeValue(String key) async {
    if(isKeyExisted(key) == false) return false;
    return await _sp.remove(key);
  }

  /// 清除所有
  Future<bool> clearAllValue() async {
    await init();
    return await _sp.clear();
  }

  /// key是否存在
  bool isKeyExisted(String key){
    if(key.isEmpty) return false;
    return _sp.containsKey(key);
  }


  @override
  Future<void>? init() async {
    _sp = await SharedPreferences.getInstance();
    return null;
  }


}